import treetaggerwrapper
from context_functions import *
import sys
from numpy import array

if len(sys.argv)<2: 
	print "\n*** usage:",sys.argv[0]," [source file path] [word file path]"
	exit()

sourcefilepath = sys.argv[1]
wordfilepath = sys.argv[2]

treetagger_dir = '/opt/TreeTagger'
language="en"
tagger = treetaggerwrapper.TreeTagger(TAGLANG=language,TAGDIR=treetagger_dir)

d = open(sourcefilepath,"r")
i = 0
table = {}
aidi = 0
for line in d:
	#print i,"-->",line,i%4
	if i%4 == 0:
		aidi = line.replace("\n","")
	elif i%4 == 2:
		table[aidi] = line.replace("\n","")
	i += 1

documents = {}
docids = []
f = open("log.txt","w")
for row in table.keys():
	tags = tagger.TagText(table[row])
	tagsl = []
	for t in tags:
		tst = t.split("\t")
		if len(tst)>2 and (tst[1] in ["NN", "NNS", "NP", "NPS", "FW"]):
			if tst[2] == "<unknown>":
				ts = tst[0]
			else:
				ts=tst[2]
			tagsl.append(ts)
	documents[row] = " " + str(tagsl).replace("['","").replace("', '"," ").replace("']","").lower() + " "
	docids.append(row)
	#print documents[r[0]]
	f.write(row + ":\t" + documents[row]+"\n\n")
f.close()

wordfile = open(wordfilepath,"r")
wordlist = []
for line in wordfile:
	if line != "\n":
		w = line.strip().replace("\n","").split("\t")[0]
		wordlist.append(w)

matrix = []
mapping = {}

for w in wordlist:
	n = []
	for idd in docids:
		d = documents[idd]
		wp = " " + w + " "
		#print w,"-->",d
		v = len(d.split(wp)) - 1
		#if v > 0:
		#	print v,wp,"-->",d
		n.append(v)
	matrix.append(n)
matrix = array(matrix)

namefile = sourcefilepath[sourcefilepath.rfind("/")+1:sourcefilepath.rfind(".")]

writeContext("contexts/"+namefile+".rcf",matrix,wordlist,docids)
writeContext("contexts/"+namefile+"-bin.rcf",matrix,wordlist,docids,bin=True)
writeContext("contexts/"+namefile+"-bin-inv.rcf",matrix.T,docids,wordlist,bin=True)

'''
for w in wordlist:
	if sum(matrix[mapping[w]]) > 0: 
		print w,sum(matrix[mapping[w]])


salida = "[Relational Context]\nDefault Name\n[Binary Relation]\nName_of_dataset\n"

for w in wordlist:
	if sum(matrix[mapping[w]]) > 0:
		salida += str(w) + " | "
salida += "\n"
for c in documents.keys():
    salida += str(c) + " | "
salida += "\n"

for w in wordlist:
	if sum(matrix[mapping[w]]) > 0: 
		for v in matrix[mapping[w]]:
			if int(v) < 1:
				salida += str(v) + " "
			else:
				salida += "1 "
		salida += "\n"

salida += "[END Relational Context]"
f = open("contexto.rcf","w")
f.write(salida);
f.close()
'''
